import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import VueCordova from 'vue-cordova'
import api from './api'
import apiIntercom from './apiIntercom'
import vuescroll from 'vue-scroll'
import 'vue-scroll/dist/vue-scroll'
//import iView from 'iview'
//import 'iview/dist/styles/iview.css'
import Moment from 'moment'
import FastClick from 'fastclick'//解决300ms延迟问题
import "babel-polyfill";//解决低版本浏览器手机不支持es6
//引入全局组件
import AllUse from './components/AllUse'
import Intercom from './components/Intercom'

//Vue.use(iView);
Vue.use(vuescroll);
Vue.use(VueCordova);
//初始化FastClick实例。在页面的DOM文档加载完成后
FastClick.attach(document.body)
Vue.use(AllUse);
Vue.use(Intercom);
Vue.prototype.$api = api;
Vue.prototype.$apiIntercom = apiIntercom;
Vue.config.productionTip = false;
// 定义全局时间戳过滤器
Vue.filter('formatDate', function(value) {
  return Moment(value).format('YYYY-MM-DD HH:mm:ss')
});

// 兼容Android 键盘弹起时，把确认订单弹窗顶上去影响布局
const h = document.body.scrollHeight  // 用onresize事件监控窗口或框架被调整大小，先把一开始的高度记录下来
window.onresize = function () { // 如果当前窗口小于一开始记录的窗口高度，那就让当前窗口等于一开始窗口的高度
  if (document.body.scrollHeight < h) {
    document.body.style.height = h;
  }
};

/** 点击事件添加音频文件
 *  用法一：给发生点击事件的元素添加 clickMusic='true'属性
 *  用法二：在绑定的如@click函数中通过 this.clickButton() 调用
 */
// 引入音频文件[重点]
import audio from "./assets/bounce.mp3";
Vue.prototype.clickButton = () => {
  let buttonAudio = document.getElementById('buttonAudio');
  buttonAudio.setAttribute('src',audio);
  buttonAudio.play();
};
document.body.addEventListener('click',function( e ){
  let event = e || window.event;
  let target = event.target || event.srcElement;
  let clickMusic = target.getAttribute('clickMusic')
  if(clickMusic==='true'){
    Vue.prototype.clickButton();
  }
  else {
    return false;
  }
});

//调用dialog alert消息提示
Vue.prototype.$alert = (message, title = '提示') => {
  if(typeof(navigator)==='undefined' || typeof(navigator.notification)==='undefined' || typeof(navigator.notification.alert)==='undefined') {
    alert(message);
    return;
  }
  navigator.notification.alert(
      message,  // message
      null,         // callback
      title,            // title
      '确定'                  // buttonName
  );
  console.log(message, title)
};

//调用百度定位封装
Vue.prototype.$getBaiduLocation = ()=> {
    let params = {
      CoorType: "gcj02",
      OpenAutoNotifyMode: true,
      Interval: 5000,
      Distance: 1,
      LocationNotify: true
    };//bd09ll,gcj02,
    return new Promise((resolve, reject) => {
      if (typeof (baidumap_location) !== "undefined") {//if判断是为了兼容浏览器调试不报错
        baidumap_location.getCurrentPosition((result) => {
          /*console.log(`baidu coo: ${result.longitude},${result.latitude}`);*/
          //理解resolve，将结果放入resolve作为参数传给成功回调的参数,如，if(num<=5){resolve(num);}else{reject('数字太大了');}
          console.log("baidumap_location Promise调用成功");
          resolve(result)
        }, (error) => {
          debugger;
          console.log("$getBaiduLocation error", error);
          reject(error)
        }, params);
      }//if end

    });
};

//调用吐司Toast
Vue.prototype.$toast = (msg,len,position,numbers) =>{
  window.plugins.toast.showWithOptions(
      {
        message: msg, //string
        duration: len, // which "short" is 2000 ms. "long" is 4000. Or specify the nr of ms yourself：900.
        position: position,//such as "bottom"
        addPixelsY: numbers  // added a negative value to move it up a bit (default 0)
      },
      function(){console.log('main js toast success')},
      function(){console.log('main js toast error ')}

  );
};

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app');

//监听cordova插件的device方法 这种方法不适应于web端运行调试
/*document.addEventListener('deviceready', function() {
  new Vue({
    router,
    store,
    render: h => h(App)
  }).$mount('#app')
}, false);*/

